package top.superflow.version.db.data

import top.superflow.db.api.DBEntity
import top.superflow.exception.RequestUnsupportedException
import top.superflow.log.Loggable
import top.superflow.version.db.EntityVersion

class DBEntityVersion (originalEntity : Any) extends EntityVersion 
with Loggable {
  notNull(originalEntity, "The originalEntity should not be nul.")
  
  def getId : String = {
    this.name.get
  }
  
  originalEntity match {
    case dbEntity : DBEntity => {
      
      this.name.compareAndSet(this.name.get, dbEntity.getEntityName)
      
      for (field <- dbEntity.getFields.get) {
        val fieldVersion = new DBFieldVersion(field)
        this.fields += (fieldVersion.name.get -> fieldVersion)
      }
      
      for (index <- dbEntity.getIndexs.get){
        val indexVersion = new DBIndexVersion(index)
        this.indexes += (indexVersion.name.get -> indexVersion)
      }
    }
      
    case _ => {
      throw new RequestUnsupportedException("Only DBEntity is supported")
    }
  }
}